﻿using System;
using System.Text;
using Amazon;
using Amazon.Glacier;
using Amazon.Glacier.Model;

namespace AmazonGlacierGUI.Core.Helpers.AmazonGlacier
{
    public abstract class AmazonGlacierHelperBase
    {
        public string AccessKey { get; set; }
        public string SecretAccessKey { get; set; }
        public string VaultName { get; set; }
        public bool IsProcessing { get; protected set; }
                
        protected AmazonGlacierClient _client;
    
        protected void InitClient()
        {
            _client = new AmazonGlacierClient(AccessKey, SecretAccessKey, GetAmazonRegionByRegion(Region));

        }
        protected string GetExceptionMessage(Exception ex)
        {
            var sb = new StringBuilder();
            while (ex != null)
            {
                sb.AppendLine(ex.Message);
                ex = ex.InnerException;
            }
            return sb.ToString();
        }


        public AmazonRegionEnum GetRegionByAmazonRegion(RegionEndpoint region)
        {
           return region == RegionEndpoint.USEast1
                           ? AmazonRegionEnum.US_East_Virginia
                           : region == RegionEndpoint.USWest1
                                 ? AmazonRegionEnum.US_West_N_California
                                 : region == RegionEndpoint.USWest2
                                       ? AmazonRegionEnum.US_West_Oregon
                                       : region == RegionEndpoint.EUWest1
                                             ? AmazonRegionEnum.EU_West_Ireland
                                             : region == RegionEndpoint.APNortheast1
                                                   ? AmazonRegionEnum.Asia_Pacific_Tokyo
                                                   : region == RegionEndpoint.APSoutheast1
                                                         ? AmazonRegionEnum.Asia_Pacific_Singapore
                                                         : AmazonRegionEnum.Asia_Pacific_Tokyo;
        }
        public RegionEndpoint GetAmazonRegionByRegion(AmazonRegionEnum region)
        {
           return region == AmazonRegionEnum.US_East_Virginia
                             ? RegionEndpoint.USEast1
                             : region == AmazonRegionEnum.US_West_N_California
                                   ? RegionEndpoint.USWest1
                                   : region == AmazonRegionEnum.US_West_Oregon
                                         ? RegionEndpoint.USWest2
                                         : region == AmazonRegionEnum.EU_West_Ireland
                                               ? RegionEndpoint.EUWest1
                                               : region == AmazonRegionEnum.Asia_Pacific_Tokyo
                                                     ? RegionEndpoint.APNortheast1
                                                     : region == AmazonRegionEnum.Asia_Pacific_Singapore
                                                           ? RegionEndpoint.APSoutheast1
                                                           : RegionEndpoint.SAEast1;
        }
        protected DescribeJobResponse GetDescribeJobResponse(string jobId)
        {
            var request = new DescribeJobRequest()
            {
                JobId = jobId,
                VaultName = VaultName
            };
            return _client.DescribeJob(request);
        }   
        public AmazonRegionEnum Region { get; set; }

      
    }

}
